package test;

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;
import org.controlsfx.control.CheckTreeView;

public class CheckTreeViewDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        TreeView<String> tv = new TreeView<String>();

        TreeItem<String> ti = new TreeItem<String>("中国");
        TreeItem<String> ti2 = new TreeItem<String>("黑龙江");
        TreeItem<String> ti31 = new TreeItem<String>("哈尔滨");
        TreeItem<String> ti32 = new TreeItem<String>("大庆");

        tv.setRoot(ti);
        ti.getChildren().add(ti2);
        ti.getChildren().add(ti31);
        ti.getChildren().add(ti32);

        //-----
        // create the data to show in the CheckTreeView
        CheckBoxTreeItem<String> root = new CheckBoxTreeItem<String>("Root");
        root.setExpanded(true);
        root.getChildren().addAll(
                new CheckBoxTreeItem<String>("Jonathan"),
                new CheckBoxTreeItem<String>("Eugene"),
                new CheckBoxTreeItem<String>("Henri"),
                new CheckBoxTreeItem<String>("Samir"));

        // Create the CheckTreeView with the data
        final CheckTreeView<String> checkTreeView = new CheckTreeView<String>();
        checkTreeView.setRoot(root);

        // and listen to the relevant events (e.g. when the checked items change).
        checkTreeView.getCheckModel().getCheckedItems().addListener(new ListChangeListener<TreeItem<String>>() {
            public void onChanged(ListChangeListener.Change<? extends TreeItem<String>> c) {
                System.out.println(checkTreeView.getCheckModel().getCheckedItems());
            }
        });



        AnchorPane an = new AnchorPane();
        an.getChildren().addAll(checkTreeView);

        Scene sc = new Scene(an);


        primaryStage.setScene(sc);
        primaryStage.setHeight(300.0);
        primaryStage.setWidth(300.0);
        primaryStage.setTitle("CheckTreeView_demo");
        primaryStage.show();
    }
}

 class test2{
    public static void main(String[] args) {
        String test = "6332526d6332526d6332526d6332513d";
        System.out.println(toByteArray(test));
        byte[] byteArray = new byte[16];
        byteArray= new byte[]{99, 50, 82, 109, 99, 50, 82, 109, 99, 50, 82, 109, 99, 50, 81, 61};
        System.out.println(toHexString(byteArray));
        System.out.println(hexStringToString(toHexString(byteArray)));

    }

     /**
      * 字节数组转成16进制表示格式的字符串
      *
      * @param byteArray
      *            需要转换的字节数组
      * @return 16进制表示格式的字符串
      **/
     public static String toHexString(byte[] byteArray) {
         if (byteArray == null || byteArray.length < 1)
             throw new IllegalArgumentException("this byteArray must not be null or empty");

         final StringBuilder hexString = new StringBuilder();
         for (int i = 0; i < byteArray.length; i++) {
             if ((byteArray[i] & 0xff) < 0x10)//0~F前面不零
                 hexString.append("0");
             hexString.append(Integer.toHexString(0xFF & byteArray[i]));
         }
         return hexString.toString().toLowerCase();
     }

     /**
      * 16进制的字符串表示转成字节数组
      *
      * @param hexString
      *            16进制格式的字符串
      * @return 转换后的字节数组
      **/
     public static byte[] toByteArray(String hexString) {
         if (StringUtils.isEmpty(hexString))
             throw new IllegalArgumentException("this hexString must not be empty");

         hexString = hexString.toLowerCase();
         final byte[] byteArray = new byte[hexString.length() / 2];
         int k = 0;
         for (int i = 0; i < byteArray.length; i++) {//因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
             byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
             byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
             byteArray[i] = (byte) (high << 4 | low);
             k += 2;
         }
         return byteArray;
     }

     /**
      * 16进制字符串转换为字符串
      *
      * @param s
      * @return
      */
     public static String hexStringToString(String s) {
         if (s == null || s.equals("")) {
             return null;
         }
         s = s.replace(" ", "");
         byte[] baKeyword = new byte[s.length() / 2];
         for (int i = 0; i < baKeyword.length; i++) {
             try {
                 baKeyword[i] = (byte) (0xff & Integer.parseInt(
                         s.substring(i * 2, i * 2 + 2), 16));
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
         try {
             s = new String(baKeyword, "gbk");
             new String();
         } catch (Exception e1) {
             e1.printStackTrace();
         }
         return s;
     }
}
